﻿#if !NETSTANDARD2_1
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dapper.Sharding
{
    internal class SapHanaClient : IClient
    {
        public SapHanaClient(DataBaseConfig config) : base(DataBaseType.SapHana, config)
        {
            ConnectionString = ConnectionStringBuilder.BuilderSapHana(config);
        }

        public override string ConnectionString { get; set; }

        public override void CloseSession(string database = null, bool closePostgres = false)
        {
            throw new NotImplementedException();
        }

        public override Task CloseSessionAsync(string database = null, bool closePostgres = false)
        {
            throw new NotImplementedException();
        }

        public override void CreateDatabase(string name, string tablespace = null, bool useGis = false, string ext = null)
        {
            var sql = $"CREATE SCHEMA {name.ToUpper()}";
            Execute(sql);
        }

        public override async Task CreateDatabaseAsync(string name, string tablespace = null, bool useGis = false, string ext = null)
        {
            var sql = $"CREATE SCHEMA {name.ToUpper()}";
            await ExecuteAsync(sql);
        }

        public override void CreateTablespace(string name, string dir)
        {
            throw new NotImplementedException();
        }

        public override Task CreateTablespaceAsync(string name, string dir)
        {
            throw new NotImplementedException();
        }

        public override void DropDatabase(string name)
        {
            var sql = $"DROP SCHEMA {name.ToUpper()}";
            Execute(sql);
        }

        public override void DropTablespace(string name)
        {
            throw new NotImplementedException();
        }

        public override Task DropTablespaceAsync(string name)
        {
            throw new NotImplementedException();
        }

        public override bool ExistsDatabase(string name)
        {
            var sql = $"SELECT COUNT(1) FROM SCHEMAS WHERE SCHEMA_NAME='{name.ToUpper()}'";
            return ExecuteScalar<bool>(sql);
        }

        public override async Task<bool> ExistsDatabaseAsync(string name)
        {
            var sql = $"SELECT COUNT(1) FROM SCHEMAS WHERE SCHEMA_NAME='{name.ToUpper()}'";
            return await ExecuteScalarAsync<bool>(sql);
        }

        public override bool ExistsTablespace(string name)
        {
            throw new NotImplementedException();
        }

        public override Task<bool> ExistsTablespaceAsync(string name)
        {
            throw new NotImplementedException();
        }

        public override DbConnection GetConn()
        {
            var conn = new Sap.Data.Hana.HanaConnection(ConnectionString);
            if (conn.State == ConnectionState.Closed)
            {
                try
                {
                    conn.Open();
                }
                catch
                {
                    conn.Dispose();
                    throw;
                }
            }
            return conn;
        }

        public override async Task<DbConnection> GetConnAsync()
        {
            var conn = new Sap.Data.Hana.HanaConnection(ConnectionString);
            if (conn.State == ConnectionState.Closed)
            {
                try
                {
                    await conn.OpenAsync();
                }
                catch
                {
#if CORE6
                    await conn.DisposeAsync();
#else
                    conn.Dispose();
#endif
                    throw;
                }
            }
            return conn;
        }

        public override string GetDatabaseScript(string name, string tablespace = null, bool useGis = false, string ext = null)
        {
            return $"CREATE SCHEMA {name.ToUpper()}";
        }

        public override void SetTablespace(string database, string tablespace)
        {
            throw new NotImplementedException();
        }

        public override Task SetTablespaceAsync(string database, string tablespace)
        {
            throw new NotImplementedException();
        }

        public override List<string> ShowDatabases()
        {
            var sql = "SELECT SCHEMA_NAME FROM SCHEMAS";
            return Query<string>(sql);
        }

        public override List<string> ShowDatabasesExcludeSystem()
        {
            return ShowDatabases();
        }

        public override void Vacuum(string dbname)
        {
            throw new NotImplementedException();
        }

        protected override IDatabase CreateIDatabase(string name)
        {
            return new SapHanaDatabase(name, this);
        }

        protected override string GetDbName(string connStr)
        {
            throw new NotImplementedException();
        }
    }
}
#endif